/*     */ package com.zimbra.cs.mailclient.imap;
/*     */ 
/*     */ import com.zimbra.cs.mailclient.CommandFailedException;
/*     */ import com.zimbra.cs.mailclient.MailException;
/*     */ import com.zimbra.cs.mailclient.ParseException;
/*     */ import com.zimbra.cs.mailclient.util.DateUtil;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.net.SocketTimeoutException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImapRequest
/*     */ {
/*     */   private final ImapConnection connection;
/*     */   private final String tag;
/*     */   private final Atom cmd;
/*     */   private List<Object> params;
/*     */   private ResponseHandler responseHandler;
/*     */   private DataHandler dataHandler;
/*     */   
/*     */   public ImapRequest(ImapConnection connection, Atom cmd)
/*     */   {
/*  42 */     this.connection = connection;
/*  43 */     this.tag = connection.newTag();
/*  44 */     this.cmd = cmd;
/*     */   }
/*     */   
/*     */   public ImapRequest(ImapConnection connection, Atom cmd, Object... params) {
/*  48 */     this(connection, cmd);
/*  49 */     for (Object param : params) {
/*  50 */       addParam(param);
/*     */     }
/*     */   }
/*     */   
/*     */   public void addParam(Object param) {
/*  55 */     if (param == null) {
/*  56 */       throw new NullPointerException();
/*     */     }
/*  58 */     if (this.params == null) {
/*  59 */       this.params = new ArrayList();
/*     */     }
/*  61 */     this.params.add(param);
/*     */   }
/*     */   
/*     */   public void setResponseHandler(ResponseHandler handler) {
/*  65 */     this.responseHandler = handler;
/*     */   }
/*     */   
/*     */   public void setDataHandler(DataHandler handler) {
/*  69 */     this.dataHandler = handler;
/*     */   }
/*     */   
/*  72 */   public String getTag() { return this.tag; }
/*  73 */   public Atom getCommand() { return this.cmd; }
/*  74 */   public List<Object> getParams() { return this.params; }
/*  75 */   public ResponseHandler getResponseHandler() { return this.responseHandler; }
/*  76 */   public DataHandler getDataHandler() { return this.dataHandler; }
/*     */   
/*     */   public boolean isAuthenticate() {
/*  79 */     return CAtom.AUTHENTICATE.atom().equals(this.cmd);
/*     */   }
/*     */   
/*     */   public boolean isIdle() {
/*  83 */     return CAtom.IDLE.atom().equals(this.cmd);
/*     */   }
/*     */   
/*     */   public boolean isSelectOrExamine() {
/*  87 */     switch (this.cmd.getCAtom()) {
/*     */     case SELECT: case EXAMINE: 
/*  89 */       return true;
/*     */     }
/*  91 */     return false;
/*     */   }
/*     */   
/*     */   public ImapResponse send() throws IOException
/*     */   {
/*     */     try {
/*  97 */       return this.connection.sendRequest(this);
/*     */     } catch (SocketTimeoutException e) {
/*  99 */       this.connection.close();
/* 100 */       throw failed("Timeout waiting for response", e);
/*     */     } catch (MailException e) {
/* 102 */       if ((e instanceof ParseException)) {
/* 103 */         this.connection.close();
/*     */       }
/* 105 */       throw failed("Error in response", e);
/*     */     } catch (IOException e) {
/* 107 */       this.connection.close();
/* 108 */       throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   public ImapResponse sendCheckStatus() throws IOException {
/* 113 */     ImapResponse res = send();
/* 114 */     checkStatus(res);
/* 115 */     return res;
/*     */   }
/*     */   
/*     */   public void checkStatus(ImapResponse res) throws IOException {
/* 119 */     if (!res.isTagged()) {
/* 120 */       throw new MailException("Expected a tagged response");
/*     */     }
/* 122 */     if (!this.tag.equalsIgnoreCase(res.getTag())) {
/* 123 */       throw new MailException("Unexpected tag in response(expected " + this.tag + " but got " + res.getTag() + ")");
/*     */     }
/*     */     
/*     */ 
/* 127 */     if (!res.isOK()) {
/* 128 */       throw failed(res.getResponseText().getText());
/*     */     }
/*     */   }
/*     */   
/*     */   public void write(ImapOutputStream out) throws IOException {
/* 133 */     out.write(this.tag);
/* 134 */     out.write(32);
/* 135 */     out.write(this.cmd.getName());
/* 136 */     if ((this.params != null) && (this.params.size() > 0)) {
/* 137 */       out.write(32);
/* 138 */       if ((this.cmd.getCAtom() == CAtom.LOGIN) && (this.params.size() > 1)) {
/* 139 */         writeData(out, this.params.get(0));
/* 140 */         out.write(32);
/* 141 */         writeUntracedList(out, this.params.subList(1, this.params.size()));
/*     */       } else {
/* 143 */         writeList(out, this.params);
/*     */       }
/*     */     }
/* 146 */     out.newLine();
/* 147 */     out.flush();
/* 148 */     out.trace();
/*     */   }
/*     */   
/*     */   private void writeUntracedList(ImapOutputStream out, List<Object> data) throws IOException {
/* 152 */     out.setPrivacy(true);
/* 153 */     writeList(out, data);
/* 154 */     out.setPrivacy(false);
/*     */   }
/*     */   
/*     */   private void writeData(ImapOutputStream out, Object data) throws IOException
/*     */   {
/* 159 */     if ((data instanceof String)) {
/* 160 */       String s = (String)data;
/* 161 */       out.write(s.length() > 0 ? s : "\"\"");
/* 162 */     } else if ((data instanceof Atom)) {
/* 163 */       ((Atom)data).write(out);
/* 164 */     } else if ((data instanceof Quoted)) {
/* 165 */       ((Quoted)data).write(out);
/* 166 */     } else if ((data instanceof Literal)) {
/* 167 */       this.connection.writeLiteral((Literal)data);
/* 168 */     } else if ((data instanceof Flags)) {
/* 169 */       ((Flags)data).write(out);
/* 170 */     } else if ((data instanceof MailboxName)) {
/* 171 */       String encoded = ((MailboxName)data).encode();
/* 172 */       writeData(out, ImapData.asAString(encoded));
/* 173 */     } else if ((data instanceof IDInfo)) {
/* 174 */       writeData(out, ((IDInfo)data).toRequestParam());
/* 175 */     } else if ((data instanceof Date)) {
/* 176 */       writeData(out, new Quoted(toInternalDate((Date)data)));
/* 177 */     } else if ((data instanceof Object[])) {
/* 178 */       writeData(out, Arrays.asList((Object[])data));
/* 179 */     } else if ((data instanceof List)) {
/* 180 */       out.write(40);
/* 181 */       writeList(out, (List)data);
/* 182 */       out.write(41);
/* 183 */     } else if ((data instanceof AppendMessage)) {
/* 184 */       writeList(out, ((AppendMessage)data).getData());
/*     */     } else {
/* 186 */       writeData(out, data.toString());
/*     */     }
/*     */   }
/*     */   
/*     */   private static String toInternalDate(Date date)
/*     */   {
/* 192 */     return DateUtil.toImapDateTime(date);
/*     */   }
/*     */   
/*     */   private void writeList(ImapOutputStream out, List<?> list) throws IOException
/*     */   {
/* 197 */     Iterator<?> it = list.iterator();
/* 198 */     if (it.hasNext()) {
/* 199 */       writeData(out, it.next());
/* 200 */       while (it.hasNext()) {
/* 201 */         out.write(32);
/* 202 */         writeData(out, it.next());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public CommandFailedException failed(String error) {
/* 208 */     return failed(error, null);
/*     */   }
/*     */   
/*     */   public CommandFailedException failed(String error, Throwable cause) {
/* 212 */     CommandFailedException cfe = new CommandFailedException(this.cmd.getName(), error);
/*     */     try {
/* 214 */       cfe.setRequest(toString());
/*     */     }
/*     */     catch (Exception e) {}
/* 217 */     cfe.initCause(cause);
/* 218 */     return cfe;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 223 */     StringBuilder sb = new StringBuilder();
/* 224 */     sb.append(this.tag).append(' ').append(this.cmd);
/* 225 */     if (this.params != null) {
/* 226 */       if ((this.cmd.getCAtom() == CAtom.LOGIN) && (this.params.size() > 1)) {
/* 227 */         sb.append(' ');
/* 228 */         append(sb, this.params.get(0));
/* 229 */         sb.append(" <password>");
/*     */       } else {
/* 231 */         for (Object param : this.params) {
/* 232 */           sb.append(' ');
/* 233 */           append(sb, param);
/*     */         }
/*     */       }
/*     */     }
/* 237 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private void append(StringBuilder sb, Object param) {
/* 241 */     if ((param instanceof String)) {
/* 242 */       String s = (String)param;
/* 243 */       sb.append(s.length() > 0 ? s : "\"\"");
/* 244 */     } else if ((param instanceof Quoted)) {
/* 245 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/*     */       try {
/* 247 */         ((Quoted)param).write(baos);
/*     */       } catch (IOException e) {
/* 249 */         throw new AssertionError();
/*     */       }
/* 251 */       sb.append(baos.toString());
/* 252 */     } else if ((param instanceof Literal)) {
/* 253 */       sb.append("<literal ");
/* 254 */       sb.append(((Literal)param).getSize());
/* 255 */       sb.append(" bytes>");
/* 256 */     } else if ((param instanceof MailboxName)) {
/* 257 */       String encoded = ((MailboxName)param).encode();
/* 258 */       append(sb, ImapData.asAString(encoded));
/* 259 */     } else if ((param instanceof IDInfo)) {
/* 260 */       append(sb, ((IDInfo)param).toRequestParam());
/* 261 */     } else if ((param instanceof Date)) {
/* 262 */       append(sb, new Quoted(toInternalDate((Date)param)));
/* 263 */     } else if ((param instanceof Object[])) {
/* 264 */       append(sb, Arrays.asList((Object[])param));
/* 265 */     } else if ((param instanceof List)) {
/* 266 */       sb.append('(');
/* 267 */       Iterator<?> it = ((List)param).iterator();
/* 268 */       if (it.hasNext()) {
/* 269 */         append(sb, it.next());
/* 270 */         while (it.hasNext()) {
/* 271 */           sb.append(' ');
/* 272 */           append(sb, it.next());
/*     */         }
/*     */       }
/* 275 */       sb.append(')');
/*     */     } else {
/* 277 */       sb.append(param.toString());
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 282 */     Date date = new Date();
/* 283 */     System.out.println("new = " + toInternalDate(date));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/imap/ImapRequest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */